package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TInDeductionStatApplyResultEntry;
import com.ims.core.doc.mapper.TInDeductionStatApplyResultMapper;
import com.ims.core.doc.domain.TInDeductionStatApplyResult;
import com.ims.core.doc.service.ITInDeductionStatApplyResultService;

/**
 * 统计表申请结果Service业务层处理
 *
 * @author IMS
 * @date 2024-12-11
 */
@Service
public class TInDeductionStatApplyResultServiceImpl implements ITInDeductionStatApplyResultService
{
    @Autowired
    private TInDeductionStatApplyResultMapper tInDeductionStatApplyResultMapper;

    /**
     * 查询统计表申请结果
     *
     * @param id 统计表申请结果主键
     * @return 统计表申请结果
     */
    @Override
    public TInDeductionStatApplyResult selectTInDeductionStatApplyResultById(Long id)
    {
        return tInDeductionStatApplyResultMapper.selectTInDeductionStatApplyResultById(id);
    }

    /**
     * 查询统计表申请结果列表
     *
     * @param tInDeductionStatApplyResult 统计表申请结果
     * @return 统计表申请结果
     */
    @Override
    public List<TInDeductionStatApplyResult> selectTInDeductionStatApplyResultList(TInDeductionStatApplyResult tInDeductionStatApplyResult)
    {
        return tInDeductionStatApplyResultMapper.selectTInDeductionStatApplyResultList(tInDeductionStatApplyResult);
    }

    /**
     * 新增统计表申请结果
     *
     * @param tInDeductionStatApplyResult 统计表申请结果
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInDeductionStatApplyResult(TInDeductionStatApplyResult tInDeductionStatApplyResult)
    {
        tInDeductionStatApplyResult.setResultId(IdUtils.simpleUUID());
        tInDeductionStatApplyResult.setCreateTime(DateUtils.getNowDate());
        int rows = tInDeductionStatApplyResultMapper.insertTInDeductionStatApplyResult(tInDeductionStatApplyResult);
        insertTInDeductionStatApplyResultEntry(tInDeductionStatApplyResult);
        return rows;
    }

    /**
     * 修改统计表申请结果
     *
     * @param tInDeductionStatApplyResult 统计表申请结果
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInDeductionStatApplyResult(TInDeductionStatApplyResult tInDeductionStatApplyResult)
    {
        tInDeductionStatApplyResult.setUpdateTime(DateUtils.getNowDate());
        tInDeductionStatApplyResultMapper.deleteTInDeductionStatApplyResultEntryByResultId(tInDeductionStatApplyResult.getId());
        insertTInDeductionStatApplyResultEntry(tInDeductionStatApplyResult);
        return tInDeductionStatApplyResultMapper.updateTInDeductionStatApplyResult(tInDeductionStatApplyResult);
    }

    /**
     * 批量删除统计表申请结果
     *
     * @param ids 需要删除的统计表申请结果主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInDeductionStatApplyResultByIds(Long[] ids)
    {
        tInDeductionStatApplyResultMapper.deleteTInDeductionStatApplyResultEntryByResultIds(ids);
        return tInDeductionStatApplyResultMapper.deleteTInDeductionStatApplyResultByIds(ids);
    }

    /**
     * 删除统计表申请结果信息
     *
     * @param id 统计表申请结果主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInDeductionStatApplyResultById(Long id)
    {
        tInDeductionStatApplyResultMapper.deleteTInDeductionStatApplyResultEntryByResultId(id);
        return tInDeductionStatApplyResultMapper.deleteTInDeductionStatApplyResultById(id);
    }

    /**
     * 新增抵扣统计明细信息
     *
     * @param tInDeductionStatApplyResult 统计表申请结果对象
     */
    public void insertTInDeductionStatApplyResultEntry(TInDeductionStatApplyResult tInDeductionStatApplyResult)
    {
        List<TInDeductionStatApplyResultEntry> tInDeductionStatApplyResultEntryList = tInDeductionStatApplyResult.getTInDeductionStatApplyResultEntryList();
        String resultId = tInDeductionStatApplyResult.getResultId();
        if (StringUtils.isNotNull(tInDeductionStatApplyResultEntryList))
        {
            List<TInDeductionStatApplyResultEntry> list = new ArrayList<TInDeductionStatApplyResultEntry>();
            for (TInDeductionStatApplyResultEntry tInDeductionStatApplyResultEntry : tInDeductionStatApplyResultEntryList)
            {
                tInDeductionStatApplyResultEntry.setEntryId(IdUtils.simpleUUID());
                tInDeductionStatApplyResultEntry.setResultId(resultId);
                list.add(tInDeductionStatApplyResultEntry);
            }
            if (list.size() > 0)
            {
                tInDeductionStatApplyResultMapper.batchTInDeductionStatApplyResultEntry(list);
            }
        }
    }
}
